from abc import ABCMeta,abstractmethod
#产品
CMD_BYTES =16

CMD_LED                     = 0x1 
CMD_MOTOR              = 0x2 
CMD_TYPE_MAX        =0x3

CMD_LED_ON  =1
CMD_LED_OFF=2
CMD_LED_HORSE    =3
CMD_MOTOR_FORWARD  =4
CMD_MOTOR_BACKWARD=5
CMD_ACTION_MAX=6

class CCmd(metaclass=ABCMeta):
    def __init__(self):
        print('CCmd')
        pass
    @abstractmethod
    def ProcessMsg(self,cmdValue):
        pass

    
class CLed(CCmd):
    def __init__(self):
        print('CLed')
        pass
    
    def ProcessMsg(self,cmdValue):
        if  cmdValue == CMD_LED_ON :
            self.LedOn()
            pass
        elif  cmdValue == CMD_LED_OFF :
            self.LedOff()
            pass            
        elif  cmdValue == CMD_LED_HORSE :
            self.LedHorse()
            pass
        
    def LedOn(self):
        print('CLed->LedOn')
        pass
    
    def LedOff(self):
        print('CLed->LedOff')
        pass
    
    def LedHorse(self):
        print('CLed->LedHorse')
        pass

class CMotor(CCmd):
    def __init__(self):
        print('CLed')
        pass
    
    def ProcessMsg(self,cmdValue):
        if  cmdValue == CMD_MOTOR_FORWARD :
            self.MotorForward()
            pass
        elif  cmdValue == CMD_MOTOR_BACKWARD :
            self.MotorBackward()
            pass
        else:
            pass
    
    def MotorForward(self):
        print('CMotor->MotorForward')
        pass
    
    def MotorBackward(self):
        print('CMotor->MotorBackward')
        pass

class CInvoker:
    def __init__(self):
        self.mCmdTable={}
        self.init()        
        pass    
    def  init(self):
        theLed = CLed()
        theMotor = CMotor()
        
        self.cmdRegister(CMD_LED,theLed)
        self.cmdRegister(CMD_MOTOR,theMotor)        
        pass
    
    def  cmdRegister(self, cmdType,   pCmdObj):        
        if  cmdType in self.mCmdTable:
            print('cmdRegister failed !!')
        else:
            self.mCmdTable[cmdType] = pCmdObj
        pass
           
    def  Invoke(self, cmdType , cmdAction):
        if  cmdType in  self.mCmdTable:
            self.mCmdTable[cmdType].ProcessMsg(cmdAction)
        pass

if __name__ == "__main__":
    theInvoker = CInvoker() 
    theInvoker.Invoke(CMD_LED,CMD_LED_HORSE);    
    theInvoker.Invoke(CMD_MOTOR,CMD_MOTOR_BACKWARD);
    
    